﻿using System.IO;
using System.Reflection;

namespace CSW.Framework.Common.Utils
{
    /// <summary>
    /// EntryAssembly information.
    /// </summary>
    public static class EntryAssembly
    {
        private static readonly string m_Location = string.Empty;
        private static readonly string m_Directory = string.Empty;
        private static readonly string m_CompanyName = string.Empty;
        private static readonly string m_ProductName = string.Empty;
        private static readonly string m_ProductVersion = string.Empty;
        private static readonly Assembly m_Assembly;

        static EntryAssembly()
        {
            // Get the directory where the entry assembly is located.
            m_Assembly = Assembly.GetEntryAssembly();
            if (m_Assembly == null) // exit if design mode
                return;

            AssemblyName entryAssemblyName = m_Assembly.GetName();

            m_Location = m_Assembly.Location;
            m_Directory = Path.GetDirectoryName(m_Location);
            m_ProductVersion = string.Format("{0}", entryAssemblyName.Version);

            // Get the entry assembly's Company and Product name.
            object[] entryAssemblyAttributes = m_Assembly.GetCustomAttributes(false);
            if (entryAssemblyAttributes != null)
            {
                foreach (object attribute in entryAssemblyAttributes)
                {
                    if (attribute is AssemblyCompanyAttribute)
                        m_CompanyName = ((AssemblyCompanyAttribute)attribute).Company;
                    else if (attribute is AssemblyProductAttribute)
                        m_ProductName = ((AssemblyProductAttribute)attribute).Product;
                }
            }

            // If the AssemblyProductAttribute is not set, use the assembly's short
            // name in its place.
            if (m_ProductName == null || m_ProductName.Trim() == string.Empty)
                m_ProductName = entryAssemblyName.Name;
        }

        /// <summary>
        /// Gets the directory where the entry assembly is located.
        /// </summary>
        /// <value>The directory where the entry assembly is located.</value>
        public static string Directory
        {
            get { return m_Directory; }
        }

        /// <summary>
        /// Gets the entry assembly.
        /// </summary>
        /// <value>The entry assembly.</value>
        public static Assembly Assembly
        {
            get { return m_Assembly; }
        }

        /// <summary>
        /// Gets the full path of the entry assembly.
        /// </summary>
        /// <value>The full path of the entry assembly.</value>
        public static string Location
        {
            get { return m_Location; }
        }

        /// <summary>
        /// Gets the company name.
        /// </summary>
        /// <value>The company name.</value>
        public static string CompanyName
        {
            get { return m_CompanyName; }
        }

        /// <summary>
        /// Gets the product name.
        /// </summary>
        /// <value>The product name.</value>
        public static string ProductName
        {
            get { return m_ProductName; }
        }

        /// <summary>
        /// Gets the product version.
        /// </summary>
        /// <value>The product version.</value>
        public static string ProductVersion
        {
            get { return m_ProductVersion; }
        }
    }
}
